﻿using System.Collections.Generic;
using SimpleOrmCFPC.Core.Model;
using SimpleOrmCFPC.upgrade;

namespace SimpleOrmCFPC.Core.RequestsComputer.Dialects.Sqlite
{
    public class SQLiteTableDialect : TableDialect
    {
        private DbToSqlTypeConverter dbToSqlTypeConverter;

        public SQLiteTableDialect(DbToSqlTypeConverter dbToSqlTypeConverter)
        {
            this.dbToSqlTypeConverter = dbToSqlTypeConverter;
        }

        public string GenerateCreateTableRequest(TableMapping table)
        {
            string request = "create table " + table.Name + " (";
            var fields = new List<string>();
            foreach (var columnInfo in table.Columns)
            {
                string decl = columnInfo.Name + " " + dbToSqlTypeConverter.ConvertType(columnInfo.Type, columnInfo.MaxLength) + " ";

                if (columnInfo.PrimaryKey)
                    decl += "primary key ";
                if (columnInfo.AutoIncrement)
                    decl += "autoincrement ";
                if (columnInfo.NotNull)
                    decl += "not null ";
                fields.Add(decl);
            }

            request += string.Join(", ", fields.ToArray()) + ")";
            return request;
        }

        
    }
}